/*
 * Copyright (C) 拾光分享网 
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 */
package top.hiai._admin.reply;

import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;

import top.hiai.common.account.UserService;
import top.hiai.common.model.Article;
import top.hiai.common.model.Reply;

/**
* @Author www.hiai.top
* @Email  goodsking@163.com
* @Message 更多资源尽在www.hiai.top
* @Introduction 类的介绍
*/

public class ReplyAdminService {
	
	public static final ReplyAdminService me = new ReplyAdminService();
	
	private Reply replyDao = new Reply().dao();
	//private Reply replyDao = new Reply().dao();

	/**
	 * 后台管理所有评论分页
	 */
	public Page<Reply> paginate(int pageNum,int pageSize) {
		Page<Reply> replyPage = replyDao.paginate(pageNum, pageSize, "select a.aid,a.title, r.rid,r.content,r.status,r.addtime,r.uid as accountId", "from bbs_reply as r ,bbs_article as a where r.aid=a.aid order by addtime desc");
		UserService.me.join("accountId", replyPage.getList(), "nickName", "avatar");
		return replyPage;
	}
	
	/**
	 * 锁定评论,就是不让用户查看
	 * @param aid
	 * @return
	 */
	public Ret lock(int rid) {
		int n = Db.update("update bbs_reply set status = ? where rid=?", Reply.STATUS_OK, rid);
		if (n > 0) {
			return Ret.ok("msg", "显示成功");
		} else {
			return Ret.fail().set("msg", "锁定失败");
		}
	}

	/**
	 * 解锁评论，就是让用户查看
	 * @param aid
	 * @return
	 */
	public Ret unlock(int rid) {
		int n = Db.update("update bbs_reply set status = ? where rid = ?", Article.STATUS_NOT , rid);
		if (n > 0) {
			return Ret.ok("msg", "解锁成功");
		} else {
			return Ret.fail().set("msg", "解锁失败");
		}
	}
	
	/**
	 * 删除评论
	 * 删除评论前需要对文章的评论字段减1
	 * @param id
	 */
	public void replyDelete(int rid,int aid){
		//删除文章前把文章的评论字段减一
		boolean temp = replyDao.deleteById(rid);
		if(temp){
			Db.update(" update bbs_article set replyCount = replyCount-1 where aid = ? ", aid);
		}
	}
	
}